import { EnumSchema } from '#/common';
import { PresetColorEnum } from '@/enums/colorEnum';
import { useI18n } from '@/hooks/web/useI18n';

/** 读写权限枚举 */
export enum ReadWritePermissionEnum {
  /** 读写 */
  ReadWrite = 0,
  /** 只读 */
  ReadOnly = 1,
  /** 只写 */
  WriteOnly = 2,
}

/** 读写权限实体 */
export class ReadWritePermissionEntity {
  /** 读写权限 */
  ReadWrite: ReadWritePermissionEnum;
  /** 显示描述 */
  Display: string;
  /** 颜色 */
  Color?: PresetColorEnum | string;

  constructor(readWrite: ReadWritePermissionEnum, color?: PresetColorEnum | string) {
    const { t } = useI18n();
    this.ReadWrite = readWrite;
    this.Color = color;
    switch (readWrite) {
      case ReadWritePermissionEnum.ReadWrite:
        // this.Display = '读写';
        this.Display = t('iot.enum.readWritePermission.readWrite');
        this.Color = this.Color ? this.Color : PresetColorEnum.Green;
        break;
      case ReadWritePermissionEnum.ReadOnly:
        // this.Display = '只读';
        this.Display = t('iot.enum.readWritePermission.readOnly');
        this.Color = this.Color ? this.Color : PresetColorEnum.Blue;
        break;
      case ReadWritePermissionEnum.WriteOnly:
        // this.Display = '只写';
        this.Display = t('iot.enum.readWritePermission.writeOnly');
        this.Color = this.Color ? this.Color : PresetColorEnum.Orange;
        break;
    }
  }
}

//#region 获取读写权限枚举架构
/**
 * 获取读写权限枚举架构
 */
export function getReadWritePermissionEnumSchema(enableTag = true): EnumSchema[] {
  const enumSchema: EnumSchema[] = [];
  const entities: ReadWritePermissionEntity[] = [
    new ReadWritePermissionEntity(ReadWritePermissionEnum.ReadWrite),
    new ReadWritePermissionEntity(ReadWritePermissionEnum.ReadOnly),
    new ReadWritePermissionEntity(ReadWritePermissionEnum.WriteOnly),
  ];
  entities.forEach((item) => {
    enumSchema.push({
      key: item.ReadWrite as number,
      label: item.Display,
      value: item.ReadWrite as number,
      tag: enableTag,
      tagColor: item.Color,
    });
  });
  return enumSchema;
}
//#endregion
